package com.cpi.cornpp.instructions.futures.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.instructions.futures.exception.InstructionsFuturesException;
import com.cpi.cornpp.instructions.futures.form.InstructionsFuturesForm;
import com.cpi.cornpp.instructions.futures.services.InstructionsFuturesService;
import com.cpi.cornpp.instructions.futures.view.InstructionsFuturesView;
import com.cpi.cornpp.period.exception.PeriodException;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;
/**
 * Purchases Instructions Futures Action
 * @author Edgar Joao
 *
 */
public class InstructionsFuturesAction extends MappingDispatchAction {
	/** Identifier default workflow*/
	private static final String DEFAULT = "default";//$NON-NLS-1$
	private static final String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private static final String RECORDS_TO_PURCHASES_INSTRUCTIONS_FUTURES = "RECORDS_TO_PURCHASES_INSTRUCTIONS_FUTURES";
	private SystemParameterCollection systemParameterCollection = null;
	private SystemParameterBean systemParameterBean = null;
	/**
	 * Metodo que inicializa las variables
	 * 
	 * @author Edgar Joao
	 * @param argMapping
	 * @param argForm
	 * @param argRequest
	 * @param argResponse
	 * @return
	 * @throws InstructionsFuturesException
	 */
	public ActionForward loadInstructionsFutures(ActionMapping argMapping, ActionForm argForm,
			HttpServletRequest argRequest, HttpServletResponse argResponse)
			throws Exception {
		HttpSession httpSession = null;
		InstructionsFuturesService service = null;				
		InstructionsFuturesForm form = null;		
		try{
			/* Inicializar Variables */
			service = new InstructionsFuturesService();
			form = (InstructionsFuturesForm) argForm;		
			form.setInstructionsFuturesView(fillList(service.getInstructionsFuturesList(34)));
			
			form.setAccountPeriod(service.periodAccountApplication());
			form.setApplicationPeriod(service.periodAccountApplication());
			
		}catch (PeriodException periodException){
			httpSession = argRequest.getSession(true);
			httpSession.setAttribute(CUSTOM_EXCEPTION, periodException);
			throw periodException;
		}catch (Exception exception){
			InstructionsFuturesException futuInstructionsFuturesException = null;
			exception.printStackTrace(System.out);
			futuInstructionsFuturesException= new InstructionsFuturesException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession = argRequest.getSession(true);
			httpSession.setAttribute(CUSTOM_EXCEPTION, futuInstructionsFuturesException);
			throw futuInstructionsFuturesException;
		}							
		return argMapping.findForward(DEFAULT);
	}
	/**
	 * Helper Utilizado para llenar la lista
	 * de vistas InstrucionsFuturesView
	 * 
	 * @author Edgar Joao
	 * @param instructionsFuturesView
	 * @return
	 */
	private List fillList(List instructionsFuturesView) {
		List list = null;
		list = new ArrayList(0);
		int totalRecordsToAdd = 0;
		InstructionsFuturesView iFuturesView = null;
		systemParameterCollection = SystemParameterCollection.getInstance();
		systemParameterBean = systemParameterCollection.getSystemParameterBean(RECORDS_TO_PURCHASES_INSTRUCTIONS_FUTURES);
		list.addAll(instructionsFuturesView);
		totalRecordsToAdd = systemParameterBean.getIntValue() - instructionsFuturesView.size();
		for( int int_i = totalRecordsToAdd; int_i>0; int_i-- ){
			iFuturesView = new InstructionsFuturesView();			
			list.add(iFuturesView);
		}		
		return list;
	}
	/**
	 * Metodo que agrega una nueva Estrategia de Compra
	 * @author Edgar Joao
	 * @param argMapping
	 * @param argForm
	 * @param argRequest
	 * @param argResponse
	 * @return
	 * @throws InstructionsFuturesException
	 */
	public ActionForward addInstructionsFutures(ActionMapping argMapping, ActionForm argForm,
			HttpServletRequest argRequest, HttpServletResponse argResponse)
			throws InstructionsFuturesException {
		HttpSession httpSession = null;
		InstructionsFuturesService service = null;				
		InstructionsFuturesForm form = null;
		try{
			form = (InstructionsFuturesForm) argForm;
			service = new InstructionsFuturesService();
			service.saveInstructionsFutures(form.getInstructionsFuturesView());
		}catch (Exception exception){
			InstructionsFuturesException futuInstructionsFuturesException = null;
			exception.printStackTrace(System.out);
			futuInstructionsFuturesException= new InstructionsFuturesException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession = argRequest.getSession(true);
			httpSession.setAttribute(CUSTOM_EXCEPTION, futuInstructionsFuturesException);
			throw futuInstructionsFuturesException;
		}					
		return null;
	}		

}
